﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TodayWhoModel;
using System.Data.SqlClient;
using InfoSoftGlobal;
using System.Data;

namespace TodayWho
{
    public partial class stats : System.Web.UI.Page
    {
  
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Session["userId"] == null)
                {
                    Response.Redirect("home.aspx");
                }
                else
                {
                    chartPageView.Text = CreateChartLastWeek();
                    chartLinkview.Text = CreateChartLastWeekLinks();
                    SetServices();
                    chartServiceDetails.Text = CreateChartServiceDetailsLastMonth();
                }
            }
        }

        private void SetServices()
        {
            UserServiceStatsModel userServices = new UserServiceStatsModel();
            userServices.UserId = Convert.ToInt32(Session["userId"].ToString());
            DataTable dt = new DataTable();
            dt.Load(userServices.RetrieveServiceIds());
            ddlServiceDetails.DataSource = dt;
            ddlServiceDetails.DataBind();
            ddlServiceDetails.SelectedIndex = 0;
            if (ddlServiceDetails.SelectedItem == null)
            {
                lblNoService.Visible = true;
            }
        }

        protected void ddlPageview_OnSelectedIndexChanged(object sender,EventArgs e)
        {
            switch (ddlPageview.SelectedValue)
            { 
                case "1":
                    chartPageView.Text=CreateChartToday();
                    break;
                case "2":
                    chartPageView.Text = CreateChartLastWeek();
                    break;
                case "3":
                    chartPageView.Text = CreateChartLastMonth();
                    break;
                case "4":
                    chartPageView.Text = CreateChartTotal();
                    break;
            }
        }
        public string CreateChartToday()
        {
            int totalCount = 0;
            //strXML will be used to store the entire XML document generated
            string strXML;

            //Generate the graph element
            strXML = "<graph caption='Sayfa Gösterim Sayısı' subCaption='Bugün' showAlternateHGridColor='1' AlternateHGridColor='016817' divLineColor='016817' ";
            strXML += " divLineAlpha='20' decimalPrecision='0' alternateHGridAlpha='5' canvasBorderColor='666666' baseFontColor='666666' lineThickness='3' bgColor='E7EEFB' ";
            strXML += " yaxisname='Kere' xaxisname='Tarih' >";
            PageviewModel pageview = new PageviewModel();
            pageview.UserId = Convert.ToInt32(Session["userId"].ToString());
            pageview.StatsDay = DateTime.Today;
            SqlDataReader reader=pageview.RetrievePageViewToday();
            
            while(reader.Read()){
            
                //Generate <set name='..' value='..' />		
                strXML += "<set name='" + Convert.ToDateTime(reader["statsDay"].ToString()).ToShortDateString() + "' value='" + Convert.ToInt32(reader["dayCount"]) +"' />";

                totalCount += Convert.ToInt32(reader["dayCount"]);
            }

            pageviewTotalCount.Text = totalCount.ToString();

            // Close Data Reader
            reader.Close();
            //Finally, close <graph> element
            strXML += "</graph>";

            //Create the chart - Pie 3D Chart with data from strXML
            return FusionCharts.RenderChartHTML("/FusionCharts/FCF_Column3D.swf?ChartNoDataText= Bu zaman araliginda sayfa gosterimi olmamistir.", "", strXML, "chartToday", "600", "400", false);
        }
        public string CreateChartLastWeek()
        {
            int totalCount = 0;
            //strXML will be used to store the entire XML document generated
            string strXML;

            //Generate the graph element
            strXML = "<graph caption='Sayfa Gösterim Sayısı' subCaption='Gecen hafta' showAlternateHGridColor='1' AlternateHGridColor='016817' divLineColor='016817' ";
            strXML += " divLineAlpha='20' decimalPrecision='0' alternateHGridAlpha='5' canvasBorderColor='666666' baseFontColor='666666' lineThickness='3' bgColor='E7EEFB' ";
            strXML += " yaxisname='Kere' xaxisname='Tarih'>";

            PageviewModel pageview = new PageviewModel();
            pageview.UserId = Convert.ToInt32(Session["userId"].ToString());
            pageview.StatsDay = DateTime.Today;
            SqlDataReader reader = pageview.RetrievePageViewLastWeek();
            while (reader.Read())
            {

                //Generate <set name='..' value='..' />		
                strXML += "<set name='" + Convert.ToDateTime(reader["statsDay"].ToString()).ToString("ddd") + "' value='" + Convert.ToInt32(reader["dayCount"]) + "' />";
                
                // increment total
                totalCount += Convert.ToInt32(reader["dayCount"]);
            }
            // Close Data Reader
            reader.Close();
            //Finally, close <graph> element
            strXML += "</graph>";


            pageviewTotalCount.Text = totalCount.ToString();
            //Create the chart - Pie 3D Chart with data from strXML
            return FusionCharts.RenderChartHTML("/FusionCharts/FCF_Column3D.swf?ChartNoDataText= Bu zaman araliginda sayfa gosterimi olmamistir.", "", strXML, "chartLastweek", "600", "400", false);
        }
        public string CreateChartLastMonth()
        {
            int totalCount = 0;

            //strXML will be used to store the entire XML document generated
            string strXML;

            //Generate the graph element
            strXML = "<graph caption='Sayfa Gösterim Sayısı' subCaption='Gecen ay' showAlternateHGridColor='1' AlternateHGridColor='016817' divLineColor='016817' ";
            strXML += " divLineAlpha='20' decimalPrecision='0' alternateHGridAlpha='5' canvasBorderColor='666666' baseFontColor='666666' lineThickness='3' bgColor='E7EEFB' ";
            strXML += " yaxisname='Kere' xaxisname='Tarih' rotateNames='1'>";

            PageviewModel pageview = new PageviewModel();
            pageview.UserId = Convert.ToInt32(Session["userId"].ToString());
            pageview.StatsDay = DateTime.Today;
            SqlDataReader reader = pageview.RetrievePageViewLastMonth();
            while (reader.Read())
            {

                //Generate <set name='..' value='..' />		
                strXML += "<set name='" + Convert.ToDateTime(reader["statsDay"].ToString()).ToString("dd MMM") + "' value='" + Convert.ToInt32(reader["dayCount"]) + "' />";

                // increment total
                totalCount += Convert.ToInt32(reader["dayCount"]);
            }
            // Close Data Reader
            reader.Close();
            //Finally, close <graph> element
            strXML += "</graph>";

            pageviewTotalCount.Text = totalCount.ToString();

            //Create the chart - Pie 3D Chart with data from strXML
            return FusionCharts.RenderChartHTML("/FusionCharts/FCF_Column3D.swf?ChartNoDataText= Bu zaman araliginda sayfa gosterimi olmamistir.", "", strXML, "chartLastMonth", "600", "400", false);
        }
        public string CreateChartTotal()
        {
            int totalCount = 0;

            //strXML will be used to store the entire XML document generated
            string strXML;

            //Generate the graph element
            strXML = "<graph caption='Sayfa Gösterim Sayısı' subCaption='Toplam' showAlternateHGridColor='1' AlternateHGridColor='016817' divLineColor='016817' ";
            strXML += " divLineAlpha='20' decimalPrecision='0' alternateHGridAlpha='5' canvasBorderColor='666666' baseFontColor='666666' lineThickness='3' bgColor='E7EEFB' ";
            strXML += " yaxisname='Kere' xaxisname='Tarih' rotateNames='1'>";

            PageviewModel pageview = new PageviewModel();
            pageview.UserId = Convert.ToInt32(Session["userId"].ToString());
            pageview.StatsDay = DateTime.Today;
            SqlDataReader reader = pageview.RetrievePageViewTotal();
            while (reader.Read())
            {

                //Generate <set name='..' value='..' />		
                strXML += "<set name='" + Convert.ToDateTime(reader["statsDay"].ToString()).ToString("dd MMM") + "' value='" + Convert.ToInt32(reader["dayCount"]) + "' />";

                // increment total
                totalCount += Convert.ToInt32(reader["dayCount"]);
            }
            // Close Data Reader
            reader.Close();
            //Finally, close <graph> element
            strXML += "</graph>";

            pageviewTotalCount.Text = totalCount.ToString();

            //Create the chart - Pie 3D Chart with data from strXML
            return FusionCharts.RenderChartHTML("/FusionCharts/FCF_Column3D.swf?ChartNoDataText= Bu zaman araliginda sayfa gosterimi olmamistir.", "", strXML, "chartTotal", "600", "400", false);
        }


        protected void ddlLinkview_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            switch (ddlLinkview.SelectedValue)
            {
                case "1":
                    chartLinkview.Text = CreateChartTodayLinks();
                    break;
                case "2":
                    chartLinkview.Text = CreateChartLastWeekLinks();
                    break;
                case "3":
                    chartLinkview.Text = CreateChartLastMonthLinks();
                    break;
                case "4":
                    chartLinkview.Text = CreateChartTotalLinks();
                    break;
            }
        }
        public string CreateChartTodayLinks()
        {
            int total = 0;
            //strXML will be used to store the entire XML document generated
            string strXML;

            //Generate the graph element
            strXML = "<graph caption='Link Tıklama Sayısı' subCaption='Bugün'  showNames='1' decimalPrecision='0' ";
            strXML += " pieSliceDepth='20' bgColor='E7EEFB' ";
            strXML += " pieRadius='200' >";
            UserServiceStatsModel userServices = new UserServiceStatsModel();
            userServices.UserId = Convert.ToInt32(Session["userId"].ToString());
            userServices.ClickDay = DateTime.Today;
            SqlDataReader reader = userServices.RetrieveServicesToday();
            
            while (reader.Read())
            {
                //Generate <set name='..' value='..' />		
                strXML += "<set name='" + reader["serviceName"].ToString() + "' value='" + Convert.ToInt32(reader["clickCount"]) + "' />";


                total += Convert.ToInt32(reader["clickCount"]);
            }
            // Close Data Reader
            reader.Close();
            //Finally, close <graph> element
            strXML += "</graph>";

            linkviewTotal.Text = total.ToString();

            //Create the chart - Pie 3D Chart with data from strXML
            return FusionCharts.RenderChartHTML("/FusionCharts/FCF_Pie3D.swf?ChartNoDataText= Bu zaman araliginda link gosterimi olmamistir.", "", strXML, "chartTodayLink", "600", "400", false);
        }
        public string CreateChartLastWeekLinks()
        {
            int total = 0;
            //strXML will be used to store the entire XML document generated
            string strXML;

            //Generate the graph element
            strXML = "<graph caption='Link Tıklama Sayısı' subCaption='Gecen hafta'  showNames='1' decimalPrecision='0' ";
            strXML += " pieSliceDepth='20' bgColor='E7EEFB' ";
            strXML += " pieRadius='200'>";
            UserServiceStatsModel userServices = new UserServiceStatsModel();
            userServices.UserId = Convert.ToInt32(Session["userId"].ToString());
            userServices.ClickDay = DateTime.Today;
            SqlDataReader reader = userServices.RetrieveServicesLastWeek();
            while (reader.Read())
            {
                //Generate <set name='..' value='..' />		
                strXML += "<set name='" + reader["serviceName"].ToString() + "' value='" + Convert.ToInt32(reader["sum"]) + "' />";


                total += Convert.ToInt32(reader["sum"]);
            }
            // Close Data Reader
            reader.Close();
            //Finally, close <graph> element
            strXML += "</graph>";

            linkviewTotal.Text = total.ToString();

            //Create the chart - Pie 3D Chart with data from strXML
            return FusionCharts.RenderChartHTML("/FusionCharts/FCF_Pie3D.swf?ChartNoDataText= Bu zaman araliginda link gosterimi olmamistir.", "", strXML, "chartLastweekLink", "600", "400", false);
        }
        public string CreateChartLastMonthLinks()
        {
            int total = 0;
            //strXML will be used to store the entire XML document generated
            string strXML;

            //Generate the graph element
            strXML = "<graph caption='Link Tıklama Sayısı' subCaption='Gecen ay'  showNames='1' decimalPrecision='0' ";
            strXML += " pieSliceDepth='20' bgColor='E7EEFB' ";
            strXML += " pieRadius='200'>";
            UserServiceStatsModel userServices = new UserServiceStatsModel();
            userServices.UserId = Convert.ToInt32(Session["userId"].ToString());
            userServices.ClickDay = DateTime.Today;
            SqlDataReader reader = userServices.RetrieveServicesLastMonth();
            while (reader.Read())
            {
                //Generate <set name='..' value='..' />		
                strXML += "<set name='" + reader["serviceName"].ToString() + "' value='" + Convert.ToInt32(reader["sum"]) + "' />";


                total += Convert.ToInt32(reader["sum"]);
            }
            // Close Data Reader
            reader.Close();
            //Finally, close <graph> element
            strXML += "</graph>";

            linkviewTotal.Text = total.ToString();

            //Create the chart - Pie 3D Chart with data from strXML
            return FusionCharts.RenderChartHTML("/FusionCharts/FCF_Pie3D.swf?ChartNoDataText= Bu zaman araliginda link gosterimi olmamistir.", "", strXML, "chartLastmonthLink", "600", "400", false);
        }
        public string CreateChartTotalLinks()
        {
            int total = 0;

            //strXML will be used to store the entire XML document generated
            string strXML;

            //Generate the graph element
            strXML = "<graph caption='Link Tıklama Sayısı' subCaption='Toplam'  showNames='1' decimalPrecision='0' ";
            strXML += " pieSliceDepth='20' bgColor='E7EEFB' ";
            strXML += " pieRadius='200' >";
            UserServiceStatsModel userServices = new UserServiceStatsModel();
            userServices.UserId = Convert.ToInt32(Session["userId"].ToString());
            userServices.ClickDay = DateTime.Today;
            SqlDataReader reader = userServices.RetrieveServicesTotal();
            while (reader.Read())
            {
                //Generate <set name='..' value='..' />		
                strXML += "<set name='" + reader["serviceName"].ToString() + "' value='" + Convert.ToInt32(reader["sum"]) + "'  />";


                total += Convert.ToInt32(reader["sum"]);
            }
            // Close Data Reader
            reader.Close();
            //Finally, close <graph> element
            strXML += "</graph>";

            linkviewTotal.Text = total.ToString();

            //Create the chart - Pie 3D Chart with data from strXML
            return FusionCharts.RenderChartHTML("/FusionCharts/FCF_Pie3D.swf?ChartNoDataText= Bu zaman araliginda link gosterimi olmamistir.", "", strXML, "chartTotalLink", "600", "400", false);
        }

        protected void ddlServiceDetails_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            chartServiceDetails.Text = CreateChartServiceDetailsLastMonth();
        }
        public string CreateChartServiceDetailsLastMonth()
        {
            int totalCount = 0;
            //strXML will be used to store the entire XML document generated
            string strXML;

            string serviceName = "";
            int serviceId = 0;
            if (ddlServiceDetails.SelectedItem != null)
            {
                serviceName = ddlServiceDetails.SelectedItem.Text;
                serviceId=Convert.ToInt32(ddlServiceDetails.SelectedValue);
            }

            //Generate the graph element
            strXML = "<graph caption='Servise tıklama Sayısı (" + serviceName  +")' subCaption='Gecen ay' showAlternateHGridColor='1' AlternateHGridColor='016817' divLineColor='016817' ";
            strXML += " divLineAlpha='20' decimalPrecision='0' alternateHGridAlpha='5' canvasBorderColor='666666' baseFontColor='666666' lineThickness='3' bgColor='E7EEFB' ";
            strXML += " yaxisname='Kere' xaxisname='Tarih' rotateNames='1'>";

            UserServiceStatsModel userServicesDetails = new UserServiceStatsModel();
            userServicesDetails.UserId = Convert.ToInt32(Session["userId"].ToString());
            userServicesDetails.ClickDay = DateTime.Today;
            userServicesDetails.ServiceId = serviceId;
            SqlDataReader reader = userServicesDetails.RetrieveServicesDetailsLastMonth();

            while (reader.Read())
            {

                //Generate <set name='..' value='..' />		
                strXML += "<set name='" + Convert.ToDateTime(reader["clickDay"].ToString()).ToString("dd MMM") + "' value='" + Convert.ToInt32(reader["clickCount"]) + "' />";
                totalCount += Convert.ToInt32(reader["clickCount"]);
            }

            serviceDetailsTotal.Text = totalCount.ToString();

            // Close Data Reader
            reader.Close();
            //Finally, close <graph> element
            strXML += "</graph>";

            //Create the chart - Pie 3D Chart with data from strXML
            return FusionCharts.RenderChartHTML("/FusionCharts/FCF_Line.swf?ChartNoDataText= Bu zaman araliginda link gosterimi olmamistir.", "", strXML, "chartLastMonth", "600", "400", false);
        }

    }

    
}